package top.majuan.study.study.servlet.football;

import top.majuan.study.study.dao.TbfootballDAO;
import top.majuan.study.study.entity.Result;
import top.majuan.study.study.entity.Tbfootball;
import top.majuan.study.study.util.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "footballaddServlet", urlPatterns = "/football/add.action")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        resp.setContentType("application/json");
        try {
            String tname = req.getParameter("tname");
            String league = req.getParameter("league");
            String gnames = req.getParameter("gnames");
            String intro = req.getParameter("intro");
            Integer champions = Integer.parseInt(req.getParameter("champions"));
            Integer cups = Integer.parseInt(req.getParameter("cups"));
            Integer fans = Integer.parseInt(req.getParameter("fans"));
            Integer goals = Integer.parseInt(req.getParameter("goals"));
            Tbfootball football = new Tbfootball();
            football.setTname(tname);
            football.setLeague(league);
            football.setGnames(gnames);
            football.setIntro(intro);
            football.setChampions(champions);
            football.setCups(cups);
            football.setFans(fans);
            football.setGoals(goals);
            int i = TbfootballDAO.insert(football);
            if (i == 1) {
                Result<String> result = Result.success("添加成功");
                writer.println(JsonUtils.stringify(result));
            } else {
                Result<String> result = Result.fail("添加失败", null);
            }
        }catch (Exception e) {
            e.printStackTrace();
            Result<String> result = Result.fail("添加发生错误", null);
            writer.println(JsonUtils.stringify(result));
        }
    }
}
